#ifndef DEVICE_H
#define DEVICE_H 1
/** @file
 * Definition of the Device class.
 */


#include "Block.h"
#include <map>		// for: std::map<K,V>


typedef							// create a shorthand
	std::map<daddr_x, Block *>	// for map<...>
		BlockMap;				// called BlockMap


/** @class Device
 * This class handles the interaction with the floppy
 * and maintains the Block cache.
 * It is the equivalent of a "disk-driver".
 */
class Device
{
public:

	/** Open a "filesystemdevice".
	 * @param	file	The name of the image file.
	 * @param	climit	The upper limit to the cache (default=infinite)
	 * */
	Device(const char  *file, size_t climit=0 );

	/** Set the device limit on blocknumbers.
	 *  By default the "device driver" figures this out itself,
	 *  but this method allows you to set it by hand.
	 *  @param	dlimit	The total number of blocks on this device.
	 *  @pre	The new limit must be equal or less to what the driver
	 * 			already knows.
	 */
	void	setLimit(daddr_x dlimit);

	/** Read a disk blok from the device.
	 *  Blocks retrieved are kept in a blockcache
	 *  for faster retrieval when read again.
	 *  @pre	The blocknumber requested must be within the device limit.
	 */  
	Block	*getBlock(daddr_x blocknumber);

	/** Reports usage statistics then deletes all blocks in the cache
	 */
	virtual ~Device();

private:

	int			fd;				// the handle to the unix filedescriptor
	daddr_x		dev_limit;		// the upperlimit of device size (or 0)

	size_t		cache_limit;	// the upper limit to the cache size (or 0)
	BlockMap	cache;			// the cache of recently read blocks

	// To gather statistics data (add more as desired)
	ulong		requests;		// getBlock request counter
};

#endif // DEVICE_H

